package ai;

import util.ArrayList;
import util.List;
import engine.GameEngine;
import engine.data.TerrainType;
import engine.data.Unit;

public class Path {

	private List coordinates;

	public Path() {
		coordinates = new ArrayList();
	}

	public Path(Node start, Node end) {
		coordinates = new ArrayList();
		Node currentNode = end;
		while (currentNode != start) {
			this.addNode(currentNode);
			currentNode = currentNode.parent;
		}
		//System.out.println(coordinates);
	}

	public void addNode(Node node) {
		coordinates.add(new short[] { node.x, node.y });
	}

	public void addNode(short x, short y) {
		coordinates.add(new short[] { x, y });
	}

	public List getCoordinatesList() {
		return coordinates;
	}

	public byte getFirstDirection(Unit unit) {
		short[] node = (short[]) coordinates.get(0);
		return PathFinding.getDirection(unit.x, unit.y, node[0], node[1]);
	}

	public String toString() {
		if (coordinates == null)
			return "AI WARNING - Path Found is null.";
		if (coordinates.size() == 0)
			return "AI WARNING - Path Found Size is zero.";
		StringBuffer res = new StringBuffer("Path Found: ");
		for (short i = 0; i < coordinates.size(); i++) {
			short x = ((short[]) coordinates.get(i))[0];
			short y = ((short[]) coordinates.get(i))[1];
			byte type = GameEngine.map.getTerrain(x, y).type;
			res.append(" [" + x + "," + y + "," + TerrainType.getName(type) + "]");
		}
		return res.toString();
	}

}
